//
//  MineViewController.m
//  GiftSpeak
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MineViewController.h"
#import "AppDelegate.h"
#import "MineCell.h"
#import "ClearCell.h"
#import "PageViewController.h"


#import "SaveViewController.h"
#import "ReHeight.pch"
#define ImageHeight 250.0f
#import "MBProgressHUD.h"

#import "FindViewController.h"

#import "SingleTon.h"
#import "MYAB.h"
#import "UIImageView+WebCache.h"


@interface MineViewController ()  <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) UIImageView *changeImage;
@property (nonatomic, retain) UIView *darkView;
@property (nonatomic, retain)UIVisualEffectView *effectView;

@property (nonatomic, retain)MBProgressHUD * hua;
@property (nonatomic, retain)UIImageView *backimage;
@property (nonatomic, retain)UIButton *night;
@property (nonatomic, assign)BOOL judge;

@property (nonatomic, assign)NSString *title;


@end

@implementation MineViewController


/**
 *  我的界面
 */

- (void)dealloc
{
    [_night release];
    [_hua release];
    [_backimage release];
    [_table release];
    [super dealloc];
}
- (void)viewDidAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    
    self.judge = YES;
    
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = arr[0];
    NSString *newPath = [path stringByAppendingPathComponent:@"night.plist"];
    if ([[NSKeyedUnarchiver unarchiveObjectWithFile:newPath] isEqualToString:@"1"]) {
        //[self.night setImage:[UIImage imageNamed:@"yejian.png"] forState:UIControlStateNormal];
        // [self.night setImage:[UIImage imageNamed:@"ye.png"] forState:UIControlStateSelected];
        self.judge = 1;
    } else {
        //[self.night setImage:[UIImage imageNamed:@"ye.png"] forState:UIControlStateNormal];
        //[self.night setImage:[UIImage imageNamed:@"yejian.png"] forState:UIControlStateSelected];
        self.judge = 0;
    }

    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [SingleTon shareSingleton].isNight ? [UIColor colorWithRed:0.115 green:0.126 blue:0.150 alpha:1.000] : [UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    self.title = @"我的";

    UIImageView *headerImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * OffWidth, 150 * OffHeight)] autorelease];
    headerImage.image = [UIImage imageNamed:@"header.jpg"];
   //[self.view addSubview:headerImage];
    
    
    [self creatTree];
}

//NSArray *array2 = [NSArray arrayWithObjects:@"关于我们", @"夜间模式", @"清除缓存", @"喜欢的礼物/喜欢的攻略", nil];
    

- (void)creatTree
{
    self.backimage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)] autorelease];
    _backimage.image = [UIImage imageNamed:@"mine.jpg"];
    _backimage.userInteractionEnabled = YES;
    [self.view addSubview:_backimage];
    
    UIView *view1 = [[[UIView alloc] initWithFrame:CGRectMake(100, 50, 15, 250)] autorelease];
    view1.backgroundColor = [UIColor colorWithRed:70 / 255.0f green:0 blue:175 / 255.0f alpha:1];
    //[self.backimage addSubview:view1];
    
    UIButton *search = [[[UIButton alloc] initWithFrame:CGRectMake(30 * OffWidth, 100 * OffHeight, 70 * OffWidth, 70 * OffHeight)] autorelease];
    [search setImage:[UIImage imageNamed:@"sousuo.png"] forState:UIControlStateNormal];
    [search addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    //[search setBackgroundColor:[UIColor colorWithRed:131 / 255.0f green:199 / 255.0f blue:238 / 255.0f alpha:1]];
    [search setBackgroundColor:[UIColor clearColor]];
    search.layer.cornerRadius = 35 * OffHeight;
    [_backimage addSubview:search];
    
    
    UIButton *shoucang = [[[UIButton alloc] initWithFrame:CGRectMake(200 * OffWidth, 120 * OffHeight, 70 * OffWidth, 70 * OffHeight)] autorelease];
    [shoucang setImage:[UIImage imageNamed:@"shou.png"] forState:UIControlStateNormal];
    [shoucang addTarget:self action:@selector(shoucangAction) forControlEvents:UIControlEventTouchUpInside];
    [shoucang setBackgroundColor:[UIColor colorWithRed:190 / 255.0f green:222 / 255.0f blue:238 / 255.0f alpha:1]];
    shoucang.layer.cornerRadius = 35 * OffHeight;
    [_backimage addSubview:shoucang];
    
    
    self.night = [[UIButton alloc] initWithFrame:CGRectMake(300 * OffWidth, 10 * OffHeight, 70 * OffWidth, 70 * OffHeight)];
    //[_night setImage:[UIImage imageNamed:@"ye.png"] forState:UIControlStateNormal];
    [_night setBackgroundColor:[UIColor clearColor]];
    if (self.judge == 1) {
        [_night setImage:[UIImage imageNamed:@"yejian.png"] forState:UIControlStateNormal];
        [_night setImage:[UIImage imageNamed:@"ye.png"] forState:UIControlStateSelected];
        
    } else if (self.judge == 0){
        [_night setImage:[UIImage imageNamed:@"ye.png"] forState:UIControlStateNormal];
        [_night setImage:[UIImage imageNamed:@"yejian.png"] forState:UIControlStateSelected];
    }
  //[_night setImage:[UIImage imageNamed:@"yejian.png"] forState:UIControlStateSelected];
    _night.layer.cornerRadius = 35 * OffHeight;
    [_night addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backimage addSubview:_night];
    
    
    UIButton *clean = [[UIButton alloc] initWithFrame:CGRectMake(180 * OffWidth, 290 * OffHeight, 70 * OffWidth, 70 * OffHeight)];
    [clean setImage:[UIImage imageNamed:@"qing.png"] forState:UIControlStateNormal];
    [clean setBackgroundColor:[UIColor colorWithRed:243 / 255.0f green:202 / 255.0f blue:243 / 255.0f alpha:1]];
    [clean addTarget:self action:@selector(cleanAction) forControlEvents:UIControlEventTouchUpInside];
    clean.layer.cornerRadius = 35 * OffHeight;
    [_backimage addSubview:clean];
    
    
    UIButton * about = [[[UIButton alloc] initWithFrame:CGRectMake(80 * OffWidth, 300 * OffHeight, 70 * OffWidth, 70 * OffHeight)] autorelease];
    
    [about setImage:[UIImage imageNamed:@"guan.png"] forState:UIControlStateNormal];
    [about setBackgroundColor:[UIColor clearColor]];
    [about addTarget:self action:@selector(aboutUsAction) forControlEvents:UIControlEventTouchUpInside];
    about.layer.cornerRadius = 35 * OffHeight;
    [_backimage addSubview:about];
    
}
- (void)searchAction
{
    FindViewController *find = [[[FindViewController alloc] init] autorelease];
    [self.navigationController pushViewController:find animated:YES];
}

- (void)shoucangAction
{
    SaveViewController *save = [[[SaveViewController alloc] init] autorelease];
    [self.navigationController pushViewController:save animated:YES];
}

//真正的夜间模式
- (void)nightAction:(UIButton *)but
{
    if (but.selected == NO) {
        but.selected = YES;

        
        [SingleTon shareSingleton].isNight = ![SingleTon shareSingleton].self;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil userInfo:nil];
        self.hua = [MBProgressHUD showHUDAddedTo:self.backimage animated:YES];
        self.hua.labelText = @"夜间模式已经关闭";
        [MYAB forMyAb:_hua];
        
        
    } else if(but.selected == YES) {
        but.selected = NO;
        if ([SingleTon shareSingleton].isNight == NO) {
            [SingleTon shareSingleton].isNight = ![SingleTon shareSingleton].isNight;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil   userInfo:nil];
            self.hua = [MBProgressHUD showHUDAddedTo:self.backimage animated:YES];
            self.hua.labelText = @"夜间模式已经打开";
            [MYAB forMyAb:_hua];
            //but.selected = YES;
        }

        
//        
//        [SingleTon shareSingleton].isNight = ![SingleTon shareSingleton].self;
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil userInfo:nil];
//        self.hua = [MBProgressHUD showHUDAddedTo:self.backimage animated:YES];
//        self.hua.labelText = @"夜间模式已经打开";
//        [MYAB forMyAb:_hua];
        
    }
    
    NSString *isNight = [NSString stringWithFormat:@"%d",[SingleTon shareSingleton].isNight];
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = arr[0];
    NSString *newPath = [path stringByAppendingPathComponent:@"night.plist"];
    [NSKeyedArchiver archiveRootObject:isNight toFile:newPath];
}

- (void)cleanAction
{
    self.title = nil;
    NSInteger size = [SDImageCache sharedImageCache].getSize;
    if (size > 1024 * 1024) {
        CGFloat floatSize = size / 1024.0 / 1024.0;
        self.title = [NSString stringWithFormat:@"%.fM",floatSize];
    } else if (size > 1024){
        CGFloat floatSize = size / 1024.0;
        self.title = [NSString stringWithFormat:@"%.fKB",floatSize];
    } else if (size > 0){
        self.title = [NSString stringWithFormat:@"%ldB", size];
    } else {
        self.title = @"0KB";
    }
    NSString *str = [NSString stringWithFormat:@"已清除%@", self.title];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
    [alert show];
    [[SDImageCache sharedImageCache] clearDisk];
}

- (void)aboutUsAction
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"如有更好建议请反馈滋滋乐团队" delegate:self cancelButtonTitle:@"确定 " otherButtonTitles:nil, nil];
    [alert show];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
